﻿using DocumentFormat.OpenXml.Office2010.Excel;
using ERP.Application.Contracts.ERPSystem.ProductionSystem;
using ERP.Application.Contracts.ERPSystem.ProductionSystem.Dto;
using ERP.Domain.Team.ProductionSystem;
using ERP.ERPSystem.ProductionSystem;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 生产系统-设备管理-点检保养项目-xzc
    /// </summary>
    public class SpotChecksController : Controller
    {
        private readonly ISpotCheckServices _spotcheckservices;

        public SpotChecksController(ISpotCheckServices spotcheckservices)
        {
            _spotcheckservices = spotcheckservices;
        }

        /// <summary>
        /// 点检保养项目视图
        /// </summary>
        /// <returns></returns>
        public IActionResult SpotChecksView()
        {
            return View();
        }
        /// <summary>
        /// 新增点检保养项目视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddSpotChecksView()
        {
            return View();
        }
        /// <summary>
        /// 更改点检保养项目视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateSpotChecksView()
        {
            return View();
        }
        /// <summary>
        /// 新增 点检保养项目
        /// </summary>
        /// <param name="spotchecksdto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddSpotChecks(SpotChecksDto spotchecksdto)
        {
            var i = await _spotcheckservices.AddSpotChecks(spotchecksdto);
            return i;
        }
        /// <summary>
        /// 更新 点检保养项目
        /// </summary>
        /// <param name="spotchecksdto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateSpotChecks(SpotChecksDto spotchecksdto)
        {
            var i = await _spotcheckservices.UpdateSpotChecks(spotchecksdto);
            return i;
        }
        /// <summary>
        /// 通过id查询到相对应的点检保养项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SpotChecks>> FindSpotChecks(int id)
        {
            var i = await _spotcheckservices.FindSpotChecks(id);
            return i;
        }
        /// <summary>
        /// 删除 点检保养项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeletedSpotChecks(int id)
        {
            await _spotcheckservices.DeletedSpotChecks(id);
        }
        /// <summary>
        /// 查询点检保养项目
        /// </summary>
        /// <param name="projectCode"></param>
        /// <param name="projectName"></param>
        /// <param name="projectType"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SpotChecks>> GetSpotChecks(string? projectCode, string? projectName, int? projectType)
        {
            return await _spotcheckservices.GetSpotChecks(projectCode, projectName, projectType);
        }
        /// <summary>
        /// 批量删除点检保养项目
        /// </summary>
        /// <param name="ids">要删除的ID列表</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> BatchDeleteSpotChecks(string ids)
        {
            try
            {
                if (string.IsNullOrEmpty(ids))
                {
                    return Json(new { success = false, message = "请选择要删除的记录" });
                }

                var idList = ids.Split(',').Select(int.Parse).ToList();
                var result = await _spotcheckservices.BatchDeleteSpotChecks(idList);
                
                if (result > 0)
                {
                    return Json(new { success = true, message = $"成功删除 {result} 条记录" });
                }
                else
                {
                    return Json(new { success = false, message = "删除失败" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = $"删除失败：{ex.Message}" });
            }
        }
    }
}
